<template>
    <div>
        <div style="margin-top: 15px">
            当前密码 <el-input v-model="oldPassword"></el-input>
        </div>
        <div style="margin-top: 15px">
            新密码 <el-input v-model="newPassword"></el-input>
        </div>
        <div style="margin-top: 15px">
            确认密码 <el-input v-model="confirmPassword"></el-input>
        </div>
        <div style="margin-top: 15px">
            <el-button type="success" @click="updatePassword">确认修改</el-button>
        </div>
    </div>
</template>

<script>
export default {
    name: "updatePassword",
    props: ["user"],
    data() {
        return {
            oldPassword: "",
            newPassword: "",
            confirmPassword: "",
        };
    },
    created() {
        console.log(this.user);
    },
    methods: {
        updatePassword() {
            if (this.oldPassword == "") {
                this.$message.error("请输入当前密码");
                return;
            }
            if (this.newPassword == "") {
                this.$message.error("请输入新密码");
                return;
            }
            if (this.confirmPassword == "") {
                this.$message.error("请输入确认密码");
                return;
            }
            if (this.oldPassword != this.user.password) {
                this.$message.error("当前密码不正确");
                return;
            }

            return this.axios
                .post("http://localhost:8082/user/updatePassword", {
                    userid: this.user.id,
                    password: this.newPassword,
                })
                .then((result) => {
                    this.$message.success("密码更新成功");
                    //将新密码保存到本地存储中
                    this.user.password = this.newPassword;
                    var temp = JSON.parse(localStorage.getItem("user"));
                    temp.content = this.user;
                    localStorage.setItem("user", JSON.stringify(temp));
                })
                .catch((error) => {
                    console.log(error);
                    this.$message.error("密码更新失败");
                });
        },
    },
};
</script>
